<?php

	namespace Phptricks\app\controllers\base;

	/**
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 * Description of base
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\basecontroller\basecontroller as basecontrollers;
	use Phptricks\system\library\database\database as DataBase;
	use Phptricks\system\library\form_validation\form_validation as form_validation;
	use Phptricks\system\library\custom_fields\custom_fields as custom_fields;
	use Phptricks\system\library\pagination\pagination as pagination;
	use Phptricks\system\library\date_time\date_time as date_time;
	use Phptricks\system\library\view\view as view;
	use Phptricks\system\library\security\security as security;
	use Phptricks\app\library\view\view_helper\view_helper as view_helper;
	use Phptricks\app\models\settings\settings as settings_model;
	use Phptricks\app\models\pages\pages as pages_model;

	class base extends basecontrollers{

		private static $_instance = null;
		public $view = null;
		public $config = null;
		public $view_helper = null;
		public $lang;
		public $all_lang;
		public $settings_model = null;
		public $pages_model = null;
		public $settings = null;
		public $security = null;
		public $form_validation = null;
		public $custom_fields = null;
		public $view_name;
		public $check_admin = false;
		public $pagination = false;
		public $_app_error = false;
		public $datetime = null;

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\app\controllers\base\base();
			}
			return self::$_instance;
		}

		public function __construct(){
			try {
				parent::__construct();
				$this->lang = $this->loader->_Language(array('system','ar'));
				$this->config = $this->_config->config_type('php');

				if (isset($this->config->db)) {
					DataBase::_initialize_configuration($this->config->db);
				}
				$this->security = security::getinstance();
				$this->form_validation = form_validation::getinstance();
				$this->custom_fields = custom_fields::getinstance();
				$this->settings_model = settings_model::getinstance();
				$this->pagination = pagination::getinstance();
				$this->datetime = date_time::getinstance();
				$this->settings = $this->settings_model->getsettings();
				$this->settings['notification'] = $this->custom_fields->decode($this->settings['notification']);
				$this->settings['default_group_settings'] = $this->custom_fields->decode($this->settings['default_group_settings']);
				$this->settings['site_status'] = $this->custom_fields->decode($this->settings['site_status']);

				$view_array = $this->_uri->_array_return['function'];
				$this->view_name = $this->_uri->_array_return['function'];
				if ($this->_check_admin() == true) {
					$view_array = explode('_', $view_array);
					$this->view_name = \implode('_', array_slice($view_array, 1));
					if (!empty($this->settings['site_default_theme_admin'])) {
						$this->config->template->template_dir = $this->config->template->site_admin_themes . $this->settings['site_default_theme_admin'] . "/";
					} else {
						$this->config->template->template_dir = $this->config->template->site_admin_themes;
					}
				} else {
					if (!empty($this->settings['site_default_theme'])) {
						$this->config->template->template_dir = $this->config->template->site_themes . $this->settings['site_default_theme'] . "/";
					}
				}
				$this->view = view::getinstance($this->config->template)->_smarty;
				$this->viewpath = \APPPATH . $this->config->template->view_dir;
				$this->_view_helper = view_helper::getinstance();
				$template = (array) $this->config->template;
				$template['public'] = \APPPATH . $this->config->template->public;
				$template['template_dir'] = \APPPATH . $this->config->template->template_dir;

				$this->config->template->public = \APPPATH . $this->config->template->public;
				$this->_set_view_name();
				$this->all_lang = $this->loader->_getfiles(\APPPATH . 'language/');
				$this->settings['home_themes'] = $this->_get_thames($this->config->template->site_themes);
				$this->settings['admin_themes'] = $this->_get_thames($this->config->template->site_admin_themes);

				$this->view->assign('settings', $this->settings);
				$this->view->assign('lang', $this->lang);
				$this->view->assign('all_lang', $this->all_lang);
				$this->view->assign('APPPATH', \APPPATH);
				$this->view->assign('template', $template);
				$this->view->assign('viewpath', $this->viewpath);
				$this->view->assign('date', $this->date_time->_date);
				
				$this->pages_model = pages_model::getinstance();
				$this->view->assign('pages', $this->pages_model->getpages());

				$this->_view_helper->_initialize($this->config->template, $this->lang);
				/**
				 * register modifiers
				 */
				$this->view->registerPlugin("modifier", 'decode', array($this->custom_fields, 'decode'));
				$this->view->registerPlugin("modifier", 'encode', array($this->custom_fields, 'encode'));
				$this->view->registerPlugin("modifier", 'get_image', array($this->_view_helper, 'get_image'));
				$this->view->registerPlugin("modifier", 'getsettingby', array($this->_view_helper, 'getsettingby'));
				$this->view->registerPlugin("modifier", 'get_date', array($this->_view_helper, 'get_date'));
				$this->view->registerPlugin("modifier", 'get_chart', array($this->_view_helper, 'get_chart'));
				$this->view->registerPlugin("modifier", 'get_hashtag', array($this->_view_helper, 'get_hashtag'));
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public function _set_view_name($viewname = ''){
			if (!empty($viewname)) {
				$this->view->assign('view_name', $viewname);
			} else {
				$this->view->assign('view_name', $this->view_name);
			}
			$this->view->assign('view_filename', $this->_uri->_array_return['class']);
		}

		public function _get_thames($dir = ''){
			if (empty($dir)) {
				$dir = \rtrim(\APPPATH . $this->config->template->template_dir, '/');
			} else {
				$dir = \rtrim(\APPPATH . $dir, '/');
			}
			$array_templates = \glob($dir . '/*');
			$templates = array();
			foreach ($array_templates as $key => $val) {
				if (\is_dir($val)) {
					$templates[] = \basename($val);
				}
			}
			if (count($templates) > 0) {
				sort($templates);
			}
			return $templates;
		}

		public function _errors($error_type = '404', $error_message = ''){
			$error_data = $this->lang['error_404'] . " : " . $this->settings['site_url'] . $error_message;
			$this->display_message('danger',$error_data ,'index/',240);
		}

		public function _check_admin(){
			if (preg_match('/^' . \cp_name . '\_/iUs', $this->_uri->_array_return['function']) == true) {
				$this->check_admin = true;
				return true;
			}
			return false;
		}

		/**
		 * 
		 * @param string $url url of redirect
		 * @param integer $time time of redirect
		 */
		public function redirect($url, $time = 0){
			if (isset($url)) {
				$url = \SITE_URL . $url;
				if ($time > 0) {
					\header("refresh:" . $time . ";url=" . $url);
					return true;
				}
				\header("Location:" . $url);
			}
		}

		/**
		 * @param string $error_type display message type
		 * @param string $error_data display message
		 * @param string $url url of redirect
		 * @param integer $time time of redirect
		 */
		public function display_message($error_type = 'success', $error_data = '', $url = '', $time = 3){
			if (!empty($url)) {
				$this->redirect($url, $time);
			}
			$this->view->assign('error', $error_data);
			$this->view->assign('error_type', $error_type);
			$this->view->display('header.tpl');
			$this->view->display('footer.tpl');
			exit();
		}

		public function set_notification(){
			$notification_data = array(
				'notification_count' => '0',
				'users' => array(
					'count_users' => '0',
					'usere_not_active' => '0',
					'usere_stop' => '0'
				),
				'trieds' => array(
					'count_trieds' => '0',
					'trieds_not_active' => '0',
					'comment_not_active' => '0'
				)
			);
			// Get users data
			$this->settings_model->select('COUNT( user_id ) AS count_users, user_stop', 'users');
			$this->settings_model->where(array(
				'user_stop' => array('0', '1')
			));
			$user_notification = $this->settings_model->execute('', '', 'user_stop', null);
			foreach ($user_notification as $key => $value) {
				if ($value['user_stop'] == '0') {
					$notification_data['users']['usere_stop'] = $value['count_users'];
				}
				if ($value['user_stop'] == '1') {
					$notification_data['users']['usere_not_active'] = $value['count_users'];
				}
			}
			$notification_data['users']['count_users'] = ($notification_data['users']['usere_not_active'] + $notification_data['users']['usere_stop']);

			// Get trieds data
			$this->settings_model->select('COUNT( tried_id ) AS count_tried, tried_type', 'trieds');
			$this->settings_model->where(array(
				'tried_type' => array('0')
			));
			$tried_notification = $this->settings_model->execute('', '', 'tried_type', null);
			$notification_data['trieds']['trieds_not_active'] = $tried_notification[0]['count_tried'];

			// Get comments data
			$this->settings_model->select('COUNT( comment_id ) AS count_comment, comment_type', 'comments');
			$this->settings_model->where(array(
				'comment_type' => array('0')
			));
			$comment_notification = $this->settings_model->execute('', '', 'comment_type', null);
			$notification_data['trieds']['comment_not_active'] = $comment_notification[0]['count_comment'];
			$notification_data['trieds']['count_trieds'] = ($notification_data['trieds']['trieds_not_active'] + $notification_data['trieds']['comment_not_active']);

			$notification_data['notification_count'] = ($notification_data['trieds']['count_trieds'] + $notification_data['users']['count_users']);
			$array_data = array(
				'notification' => $this->custom_fields->encode($notification_data)
			);
			$this->settings_model->updatesettings($array_data);
		}
		
		
		public function __destruct(){
//			pr($this->settings_model->db->_list_query,1);
		}

	}
	